﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.UI;
using System.Web.UI.WebControls;

namespace webAppDemo
{
    public class HtmlReportingService
    {
        public Literal BuildField(string Text, string Label, string Value)
        {
            var result = new Literal();
            result.Text = @"<div style='display:inline;'>" + Text + @"</div>";
            return result;  
        }
        public void BuildH<TTarget, TSource, TKey>(
            TTarget  target, 
            IEnumerable<TSource> items, 
            Func<TSource, TKey> GroupKeySelector, 
            Func<TSource, string> TextSelector ,
            Func<TSource, string> LabelSelector = null,            
            Func<TSource, string> ValueSelector = null,
            Func<string , string , string, Literal> LiteralContent = null) where TTarget : IRadPanelItemContainer
        {            
            var groups = items.GroupBy(GroupKeySelector);
            foreach (var group in groups)
            {
                RadPanelItem panel = new RadPanelItem(group.Key.ToString());
                foreach (var item in group)
                {
                    RadPanelItem itempanel = new RadPanelItem();
                    if (LiteralContent != null)
                    {
                        itempanel.Text = LiteralContent(
                                TextSelector != null ? TextSelector(item) : null,
                                LabelSelector != null ? LabelSelector(item) : null,
                                ValueSelector != null ? ValueSelector(item) : null).Text;                                               
                    }
                    else
                    {                        
                        itempanel.Text = TextSelector(item);                        
                    }
                    panel.Items.Add(itempanel);
                } 
                
                target.Items.Add(panel);
            }            
        }
       
    }
}